package com.info.zhiduoduo.weixin.service;

import com.info.zhiduoduo.common.configs.PayConfig;
import com.info.zhiduoduo.common.configs.ZddUrlConfig;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.constants.system.UserType;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.RandomUtil;
import com.info.zhiduoduo.common.utils.StringUtil;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.product.OrderProductWxDto;
import com.info.zhiduoduo.repository.dto.product.OrderUserInfoDto;
import com.info.zhiduoduo.repository.entity.maker.Maker;
import com.info.zhiduoduo.repository.entity.order.OrderProduct;
import com.info.zhiduoduo.repository.entity.order.Product;
import com.info.zhiduoduo.repository.mapper.order.OrderProductMapper;
import com.info.zhiduoduo.repository.mapper.order.ProductMapper;
import com.info.zhiduoduo.weixin.formbean.taxer.AiWxpayFormBean;
import com.info.zhiduoduo.weixin.formbean.taxer.WXPayFromBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class ProductService extends BaseService {

    @Resource
    ProductMapper productMapper;
    @Resource
    OrderProductMapper orderProductMapper;
    @Autowired
    ZddUrlConfig zddUrlConfig;
    @Autowired
    PayConfig payConfig;

    /**
     * 产品支付接口
     *
     * @param formBean
     * @return
     */
    public SmartResult submitPay(BaseSimpleFormBean<AiWxpayFormBean> formBean) {
        int idtype = formBean.getIdtype();
        int userid = formBean.getUserid();

        AiWxpayFormBean reqdata = formBean.getReqdata();
        if (reqdata == null) return errorParamSmartResult();

        String phoneNo = reqdata.getPhoneNo();
        Integer productId = reqdata.getProductId();
        Maker maker = new Maker().selectById(userid);
        if (maker == null) return smartResult(StatusMsg.StatusMsg_201);

        String openid = "";
        if (idtype == UserType.MAKER) {
            openid = maker.getOpenid();
        } else {
            return errorParamSmartResult();
        }

        if (StringUtil.isEmpty(openid)) {
            return errorParamSmartResult();
        }

        Product product = productMapper.selectProductById(productId);

        /** 订单类型： 1： 订单  2: 创客 开票 3:线下项目开票费用（方便后期扩展多种微信支付的场景，识别不同的订单编号） 4:购买外部产品 */
        int orderType = 4;
        String orderDesc = "产品购买";

        //支付以分为单位
        BigDecimal orderPrice = product.getNewPrice();
        String orderno = RandomUtil.createRandomNo("WXPAY_", "");
        new OrderProduct().setOrderno(orderno)
                .setProductId(productId)
                .setProductName(product.getProductName())
                .setPhoneNo(phoneNo)
                .setUserid(userid)
                .setPrice(product.getNewPrice())
                .insert();

        WXPayFromBean wxPayFromBean = new WXPayFromBean().setOpenid(openid)
                .setOrderNo(orderno)
                .setOrderPrice(orderPrice.multiply(new BigDecimal("100")).intValue())
                .setOrderDesc(orderDesc)
                .setOrderType(orderType);
        RestTemplate rest = new RestTemplate();
        return rest.postForObject(zddUrlConfig.getBasePayUrl() + payConfig.getWxpayUrl(), wxPayFromBean, SmartResult.class);
    }

    /**
     * 产品订单接口
     * admin端做个订单列表（展示订单时间、金额、手机号，状态）其中状态包含：待确认/已确认，支付完成的订单需要客户在后台点确认的，所以再加一个确认接口
     * 然后移动端也加个接口返回订单信息，包括上面这2个日期
     *
     * @param formBean
     * @return
     */
    public SmartResult listOrderByUser(BaseSimpleFormBean formBean) {
        List<OrderProductWxDto> orderProductWxDtos = orderProductMapper.listOrderByUser(formBean.getUserid());
        return SmartResult.success(orderProductWxDtos);
    }

    /**
     * 姓名：李**，手机号：138****1234
     * 姓名我就取登录人姓名手机号
     *
     * @param formBean
     * @return
     */
    public SmartResult listOrderUserInfo(BaseSimpleFormBean formBean) {
        List<OrderUserInfoDto> orderUserInfoDtos = orderProductMapper.listOrderUserInfo();
        return SmartResult.success(orderUserInfoDtos);
    }
}
